//  可视区高度
var clientHeight = 0,
    //  可视区宽度
    clientWidth = 0,
    //  容器
    containerEl = $("#container");


/**
 * 公共方法
 * @type {{resetStyle: resetStyle}}
 */
var common = {
    //  初始化
    _init() {
        this.event();
        this.resetStyle();
    },
    //  事件绑定
    event() {
        $(window).change(common.resetStyle);
    },
    //  当窗口改变.重新计算宽高度
    resetStyle: function () {
        clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
        clientWidth = document.documentElement.clientWidth || document.body.clientWidth;
        containerEl.css({"minHeight": clientWidth * 1.77 > clientHeight ? clientHeight : clientWidth * 1.77});
    },
    versions: function () {
        var u = navigator.userAgent, app = navigator.appVersion;
        return {
            trident: u.indexOf('Trident') > -1, //IE内核
            presto: u.indexOf('Presto') > -1, //opera内核
            webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
            gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐内核
            mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
            android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端
            iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
            iPad: u.indexOf('iPad') > -1, //是否iPad
            webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
            weixin: u.indexOf('MicroMessenger') > -1, //是否微信 （2015-01-22新增）
            qq: u.match(/\sQQ/i) == " qq" //是否QQ
        };
    }(),
};
common._init();


/**
 * http请求类
 */
var http_client = {
    hostname: "/appApi",
    // hostname:"http://39.106.206.183",
    request_type: {
        GET: 1,
        POST: 2
    },
    request_status: {
        SUCCESS: 0
    },
    send_flag: true,
    _convertStringToObject: function (string) {
        if (typeof string !== 'string') return null;
        let _convert_items = string.split('&'), return_data = {};
        for (let item in _convert_items) {
            let item_data = _convert_items[item], index = 0;
            item_data.indexOf("?") === 0 || item_data.indexOf("?") === 1 ? index = 1 : index = 0;
            item_data = item_data.split("=");
            return_data[item_data[0].substr(index)] = item_data[1];
        }
        return return_data;
    },
    _convertObjectToString: function (object) {
        if (typeof object !== "object") return null;
        let return_str = '';
        for (let item in object) {
            return_str += (item + "=" + object[item] + "&")
        }
        return return_str.substring(0, return_str.length - 1);
    },
    send: function (url, data, request_type, call_back, error_back) {
        let request_data = {
            headers: {
                uid: common.uid,
                token: common.access_token
            }
        };
        switch (request_type) {
            case http_client.request_type.POST:
                request_data.type = "post";
                request_data.data = data;
                request_data.url = this.hostname + url;
                break;
            default:
                request_data.type = "get";
                request_data.url = this.hostname + url + "?" + this._convertObjectToString(data);
                break;
        }
        let XHR = $.ajax(request_data);
        XHR.done(function (reqText) {
            if (typeof reqText === "string") {
                reqText = JSON.parse(reqText);
            }
            if (reqText.code === http_client.request_status.SUCCESS) {
                call_back(reqText.data);
            } else {
                if (error_back) error_back(xhr.responseText);
            }
        });
        XHR.fail(function (xhr, status, error) {
            if (error_back) error_back(error ? error : xhr.responseText);
        });
    },
    getQueryString() {
        let request_uri = window.location.href.substr(window.location.href.indexOf("?") + 1), return_data = {};
        $(request_uri.split("&")).each(function (index, item) {
            let data = item.split("=");
            return_data[data[0]] = data[1]
        });
        return return_data;
    },
};